package client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import compositePattern.SecurityCompositePatternComponent;
import compositePattern.SecurityCompositePatternComposite;
import compositePattern.SecurityCompositePatternLeaf;
import compositePattern.TransparentCompositePatternComponent;
import compositePattern.TransparentCompositePatternComposite;
import compositePattern.TransparentCompositePatternLeaf;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class CompositePatternClient{
	static JComboBox<String> jcbPattern,jcbOperation,jcbComponent;
	static JLabel labResult;
	public static void main(String[] args) {
		
		/*
		 * main code for composite pattern method
		 */
		
		final SecurityCompositePatternComponent sComposite = new SecurityCompositePatternComposite();
		final SecurityCompositePatternComponent sLeaf = new SecurityCompositePatternLeaf();
		final TransparentCompositePatternComponent tComposite = new TransparentCompositePatternComposite();
		final TransparentCompositePatternComponent tLeaf = new TransparentCompositePatternLeaf();
		ActionListener actionListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				//check for JComboBox
				switch(jcbPattern.getSelectedIndex())
				{
				case 0://SecurityCompositePattern
					switch(jcbComponent.getSelectedIndex())
					{
						case 0://leaf
							switch(jcbOperation.getSelectedIndex())
							{
							case 0:
								labResult.setText(sLeaf.operation(sComposite));
								break;
							case 1:
								labResult.setText("安全性组合模式中leaf没有这个函数");
								break;
							case 2:
								labResult.setText("安全性组合模式中leaf没有这个函数");
								break;
							case 3:
								labResult.setText("安全性组合模式中leaf没有这个函数");
								break;
							}
							break;
						case 1://composite
							switch(jcbOperation.getSelectedIndex())
							{
							case 0:
								labResult.setText(sComposite.operation(sLeaf));
								break;
							case 1:
								labResult.setText(((SecurityCompositePatternComposite) sComposite).add(sLeaf));
								break;
							case 2:
								labResult.setText(((SecurityCompositePatternComposite) sComposite).update(sLeaf));
								break;
							case 3:
								labResult.setText(((SecurityCompositePatternComposite) sComposite).remove(sLeaf));
								break;
							}
							break;
					}
					break;
					
				case 1://TransparentCompositePattern
					switch(jcbComponent.getSelectedIndex())
					{
						case 0://leaf,inside the switch will catch exception and print the exception message on view
							switch(jcbOperation.getSelectedIndex())
							{
							case 0:
								labResult.setText(tLeaf.operation(tComposite));
								break;
							case 1:
								try
								{
									labResult.setText(tLeaf.add(tComposite));
								} catch (Exception e)
								{
									labResult.setText(e.getMessage());
								}
								break;
							case 2:
								try
								{
									labResult.setText(tLeaf.update(tComposite));
								} catch (Exception e)
								{
									labResult.setText(e.getMessage());
								}
								break;
							case 3:
								try
								{
									labResult.setText(tLeaf.remove(tComposite));
								} catch (Exception e)
								{
									labResult.setText(e.getMessage());
								}
								break;
							}
							break;
						case 1://composite
							switch(jcbOperation.getSelectedIndex())
							{
							case 0:
								labResult.setText(tComposite.operation(tLeaf));
								break;
							case 1:
								try
								{
									labResult.setText(tComposite.add(tComposite));
								} catch (Exception e)
								{
									labResult.setText(e.getMessage());
								}
								break;
							case 2:
								try
								{
									labResult.setText(tComposite.update(tComposite));
								} catch (Exception e)
								{
									labResult.setText(e.getMessage());
								}
								break;
							case 3:
								try
								{
									labResult.setText(tComposite.remove(tComposite));
								} catch (Exception e)
								{
									labResult.setText(e.getMessage());
								}
								break;
							}
							break;
					}
					break;
				}
				
			}
		};
		
		/*
		 * From now on is view		
		 */
		
		//main view
		JFrame mainFrame = new JFrame("CompositePatternClient");
		mainFrame.setBounds(100, 100, 400, 300);
		JPanel mainPanel = new JPanel();
		mainPanel.setBounds(0, 0, 400, 300);
		mainFrame.setMaximumSize(new Dimension(400,300));
		mainFrame.setMinimumSize(new Dimension(400,300));
		jcbOperation = new JComboBox<String>();
		jcbOperation.addItem("Operation");
		jcbOperation.addItem("Add");
		jcbOperation.addItem("Update");
		jcbOperation.addItem("Remove");
		jcbComponent = new JComboBox<String>();
		jcbComponent.addItem("Leaf");
		jcbComponent.addItem("Composite");
		mainPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		//widget of view
		JLabel labSelectPattern = new JLabel("选择模式");
		mainPanel.add(labSelectPattern);
		jcbPattern = new JComboBox<String>();
		jcbPattern.addItem("安全性组合模式");
		jcbPattern.addItem("透明组合模式");
		mainPanel.add(jcbPattern);
		JLabel labSelectComponent = new JLabel("选择Component");
		mainPanel.add(labSelectComponent);
		mainPanel.add(jcbComponent);
		JLabel labSelectOperation = new JLabel("选择操作");
		mainPanel.add(labSelectOperation);
		mainPanel.add(jcbOperation);
		JButton btn = new JButton("运行");
		
		//listener
		btn.addActionListener(actionListener);
		mainPanel.add(btn);
		labResult = new JLabel("等待输入");
		mainPanel.add(labResult);
		
		//show view
		mainFrame.getContentPane().add(mainPanel);
		mainFrame.setVisible(true);
		
	}
}
